<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 */
class UsersController extends AppController {

/**
 * index method
 *
 * @return void
 */
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('forgetpassword','index', 'add');
	}
	
    public function login() {
		$this->layout="";
		$arr=$this->Session->read("Auth");
			
		if(!empty($arr["User"])){
			
			//$this->redirect($this->Auth->redirect());
		}
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$isAdmin =AuthComponent::user('access_level_id');
				try {
				$useId =AuthComponent::user('id');
				
				Controller::loadModel('Loginlog');
				$data["Loginlog"]["user_id"]=$useId;
				$data["Loginlog"]["ip"]=$_SERVER["REMOTE_ADDR"];
				$data["Loginlog"]["type"]="1";
				$this->Loginlog->save($data);
				}catch (Exception $e){}
			
				$this->redirect(array("controller"=>'configurations','action' => 'theme'));
					/*if($isAdmin)
				$this->redirect($this->Auth->redirect());*/
			} else {
				$this->Session->setFlash(__('Error username or password'), 'default',array("class"=>"mws-form-message error"));
			}
		}
	}
	public function logout() {
		try {
			   Controller::loadModel('Loginlog');
			   $useId =AuthComponent::user('id');
				$data["Loginlog"]["user_id"]=$useId;
				$data["Loginlog"]["ip"]=$_SERVER["REMOTE_ADDR"];
				$data["Loginlog"]["type"]="0";
				$this->Loginlog->save($data);
		}catch (Exception $e){}
				$this->redirect($this->Auth->logout());
	}
	
    public function isAuthorized($user) {
		$isAdmin =AuthComponent::user('access_level_id');
		if($isAdmin==0){
           return true;
		}
		if (in_array($this->action, array('login', 'logout',"viewmessage","printmydata","remessage","semessage","sendmessage", 'profile',"viewtime","viewalltimes","deletetime","viewjson","newmaterial","mydata"))) {
			return true;
		}
		return parent::isAuthorized($user);
	}
	
	public function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid data'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
		
		 if(isset($this->request->data["User"]["user_id"])){
		        $useId =AuthComponent::user('id');		
		       // $useId ="1";				
				$this->request->data["User"]["user_id"]=$useId;
				}
				
			$this->User->create();
			if ($this->User->save($this->request->data)) {
				$id=$this->User->id;
				$this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		}
		$users = $this->User->find('list');
		$this->set(compact('users'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
		
		 if(isset($this->request->data["User"]["user_id"])){
		       $useId =AuthComponent::user('id');		
		        //$useId ="1";				
				$this->request->data["User"]["user_id"]=$useId;
				}
				
			if ($this->User->save($this->request->data)) {
			      $this->savelog($id);
				$this->Session->setFlash(__('Data has been saved'), 'default',array("class"=>"mws-form-message success"));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Data could not be saved. Please, try again.'), 'default',array("class"=>"mws-form-message error"));
			}
		} else {
			$this->request->data = $this->User->read(null, $id);
		}
		$users = $this->User->User->find('list');
		$this->set(compact('users'));
	   $this->render("add");
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->User->id = $id;
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->User->delete()) {
		       $this->savelog($id);
				
			$this->Session->setFlash(__('Data has been deleted'), 'default',array("class"=>"mws-form-message success"));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Data was not deleted'), array('action' => 'index'), array('action' => 'index'));
		$this->redirect(array('action' => 'index'));
	}
}
